import { create, update, remove, query, createRaw } from '../../../framework/service/index';
import { Toast } from 'antd-mobile';
import { ToastTips } from '../../../common/toastTips/tosat';


export default {

  namespace: 'personal',

  state: {
    registeredUserLoginName:'',
    profile: {
      avatar: '',
      name: '',
      age: '',
      sex: '',
      height: '',
      phone: '',
      email: ''
    },
    base64Url:'',
    resume: {},
    personalData: {},
    resumeModel:{},
    province:'',
    city:'',
    district:'',
    myBalanceInfo:'',
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if(location.pathname === '/personal') {
          dispatch({
            type: 'query',
          })
          dispatch({
            type: 'getBalance',
          })

          dispatch({
            type: 'allPartTime/querySuccess',
            payload:{
              isShow:-1,
              ordeyIndex:0,
              areaIndex:0,
              typeNameIndex:0,
              key:'',
              typeId:'',
              district :'',
            }
          })
        }
      })
    },
  },

  effects: {
    *query({ payload }, { call, put }) {  // eslint-disable-line
      const { code, data, message } = yield call(query, '/api/applicants/details', {});
      // console.log('message', JSON.stringify(message));

      if ( code == 200 && data ) {
        // console.log('个人信息',JSON.stringify(data));
        yield put({
          type: 'querySuccess',
          payload: {
            personalData: data,
            profile: data.profile,
            resume: data.resume,
            resumeModel:data.resumeModel,
            province:data.gpsDistance.province,
            city:data.gpsDistance.city,
            district:data.gpsDistance.district,
            registeredUserLoginName:data.registeredUserLoginName,
          }
        })

        yield put({
          type: 'jobIntention/querySuccess',
          payload: {
            selectJobType: data.jobPlanTypes,
            selectworkingArea: data.areas,
          }
        })

        const intentionData = {
          types: data.jobPlanTypes,
          areas: data.areas,
        }

        yield put({
          type: 'myResume/querySuccess',
          payload: {
            intentionData
          }
        })
      }
    },

    // *onUploadImg({ payload }, { call, put,select }) {
    //
    //   const profile = yield select (({personal}) => personal.profile);
    //   const { code, data, message } = yield call(createRaw, '/api/upload64?blur=false', payload.base64Url);
    //   if ( code == 200 && data ) {
    //     console.log('上传图片',data);
    //     const imgData = {avatar: data.url}
    //
    //     const result =  Object.assign(profile, imgData);
    //     console.log('修改图片', result);
    //     yield put({ type: 'querySuccess',payload:{profile:result}});
    //   }
    //   console.log(code);
    //   console.log(data);
    //   console.log('onUploadImg');
    //   console.log(payload);
    //   // yield put({ type: 'querySuccess',payload:{imgURL:payload.imgURL}});
    // },

    //保存基本信息
    *onSubmit({ payload }, { call, put, select }) {

      const { profile, base64Url, province, city, district } = yield select (({personal}) => personal);

      // console.log('profile', JSON.stringify(profile));
      // console.log('base64Url', JSON.stringify(base64Url));
      // return;

      if (profile.avatar && base64Url) {
        console.log('上传图片');
        const { code, data, message } = yield call(createRaw, '/api/upload64?blur=false', base64Url);
        if ( code == 200 && data ) {
          console.log('上传成功');
          profile.avatar = data.url;
        }else{
          ToastTips('上传头像失败', 2000, null);
          console.log('上传头像失败', JSON.stringify(message));
          return;
        }
      }
      profile.province = province;
      profile.city = city;
      profile.district = district;

      // console.log('profile', JSON.stringify(profile));

      const responseData = yield call(update, '/api/applicant/profile', profile);

      // console.log('responseData', JSON.stringify(responseData));

      if ( responseData.code == 200) {
        ToastTips('保存成功', 2000, null);
        history.go(-1);
      }else{
        ToastTips('保存失败', 2000, null);
      }
    },

    //获取账号余额
    *getBalance({ payload }, { call, put, select }) {
      const { code, data, message } = yield call(query, '/api/applicants/balance', {});
      if(code == 200){
        yield put({
          type: 'querySuccess',
          payload: {
            myBalanceInfo: data
          }
        })
      }
    },

  },

  reducers: {
    querySuccess(state, action) {
      return { ...state, ...action.payload };
    },
  }

};
